// service.js文件
import Vue from 'vue'
import axios from 'axios'
import https from '@/config/index.config.js'
let baseURL = https.baseUrl
const service = axios.create({
    withCredentials: true,
    crossDomain: true,
    baseURL,
    timeout: 6000
})
// request拦截器,在请求之前做一些处理
// service.interceptors.reponse.use(
//     config => {
// 		console.log(config)
// 		// let data = {
// 		// 	'weixinAppId': https.weixinAppId,
// 		// 	'projectId': https.projectId
// 		// }
// 		// index.index(data)
//         if (store.state.token) {
//             // 给请求头添加user-token
//             config.headers["user-token"] = store.state.token;
//         }
//         return config;
//     },
//     error => {
//         return Promise.reject(error);
//     }
// );

//配置成功后的拦截器
service.interceptors.response.use(res => {
    if (res.data.data.state == 1) {
        return res.data
    } else {
		return res.data
		// uni.reLaunch({
		// 	url:'/pages/stopPage/stopPage'
		// })
  //       return Promise.reject(res.data.msg);
    }
}, error => {
    return Promise.reject(error)
})


axios.defaults.adapter = function(config) { //自己定义个适配器，用来适配uniapp的语法
    return new Promise((resolve, reject) => {
        var settle = require('axios/lib/core/settle');
        var buildURL = require('axios/lib/helpers/buildURL');
        uni.request({
            method: config.method.toUpperCase(),
            url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),
            header: config.headers,
            data: config.data,
            dataType: config.dataType,
            responseType: config.responseType,
            sslVerify: config.sslVerify,
            complete: function complete(response) {
                response = {
                    data: response.data,
                    status: response.statusCode,
                    errMsg: response.errMsg,
                    header: response.header,
                    config: config
                };

                settle(resolve, reject, response);
            }
        })
    })
}
export default service